<?php
namespace app\houtai\controller\user;
use app\houtai\controller\BaseController;
use think\Db;
use think\Request;

class LoginController extends BaseController {
  /**
   *  @brief  : 用户管理-用户登录
   *  @return : page
   *  @details: 
   */
  public function indexAction() {
    $conds = [];
    $order = [];
    $LOGIN_TYPE = config('LOGIN_TYPE');
    $CHANNELS   = $this->GetAdminChannel();
    //////////////////////////////////////////////////////////////////////////////////////
    $Channel = $this->request->param('Channel/d', -1);
    if ($Channel > 0 && array_key_exists($Channel, $CHANNELS)) {
      $conds['b.Channel'][] = ['IN', array_keys($CHANNELS)];
      $conds['b.Channel'][] = ['EQ', $Channel];
    } else {
      $conds['b.Channel'] = ['IN', array_keys($CHANNELS)];
    }
    ////////////////////////////
    $time1 = $this->request->param('time1/s', '', 'trim');
    $time2 = $this->request->param('time2/s', '', 'trim');
    
    $aa = YmdHis2Unix($time1);
    $bb = YmdHis2Unix($time2);
    $ta = date('Y-m-d 00:00:00', $aa);
    $tb = date('Y-m-d 23:59:59', $bb);
    if ($aa > 0) {
      if ($bb > 0) {
        if ($aa <= $bb) {
          $conds['a.LoginTime'] = ['BETWEEN TIME', [$ta, $tb]];
        } else {
          $conds['a.LoginTime'] = ['>= TIME', $ta];
        }
      } else {
        $conds['a.LoginTime'] = ['>= TIME', $ta];
      }
    } else {
      if ($bb > 0) {
        $conds['a.LoginTime'] = ['<= TIME', $tb];
      }
    }
    ////////////////////////////
    $sex = $this->request->param('sex/d', -1); // 0=未知; 1=男性; 2=女性; -1=所有;
    if (in_array($sex, [0, 1, 2])) {
      $conds['b.Sex'] = $sex;
    }
    ////////////////////////////
    $type = $this->request->param('type/d', -1);
    if ($type > 0 && array_key_exists($type, $LOGIN_TYPE)) {
      $conds['a.LoginType'] = $type;
    }
    ////////////////////////////
    $search = $this->request->param('search/s', '', 'trim');
    if (!empty($search)) {
      if (is_numeric($search)) {
        $search = $this->request->param('search/d', 0);
        if ($search > 0) {
          $conds['b.UserID|b.GameID'] = $search;
        }
      } else {
        $conds['b.NickName'] = ['LIKE', "%{$search}%"];
      }
    }
    //////////////////////////////////////////////////////////////////////////////////////
    $order_map = [
      'A' => 'Channel',
      'B' => 'RecordID',
      'C' => 'GameID',
      'D' => 'LoginType',
      'E' => 'MoorMachine',
      'F' => 'OpenID',
      'G' => 'LoginIP',
      'H' => 'LoginTime',
    ];
    $this->orderField = array_key_exists($this->orderField, $order_map) ? $this->orderField : 'B';
    $order[$order_map[$this->orderField]] = $this->orderDirection;
    //////////////////////////////////////////////////////////////////////////////////////
    $list = table('CYRecordDB.dbo.RecordUserLoginLog a', 'rydb.record')->field([
      'a.RecordID', 'a.LoginType', 'a.LoginTime', 'a.MoorMachine', 'a.LoginIP', 'a.OpenID',
      'b.UserID', 'b.GameID', 'b.Accounts', 'b.NickName', 'b.AccountsType', 'b.Channel',
      'b.RegisterIP', 'b.LastLogonIP', 'b.GameLogonCount', 'b.LastLogonDate', 'b.RegisterDate',
      'b.RegisterMobile', 'b.Compellation', 'b.PassPortID', 'b.Sex', 'b.AccountImgUrl',
    ])
    ->join('CYBasicDB.dbo.AccountsInfo b',   'b.UserID = a.UserID', 'LEFT')
    ->where($conds)->order($order)
    ->paginate();
    //////////////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'list'  => $list,
      'search'=> $search,
      'sex'   => $sex,
      'type'  => $type,
      'time1' => $time1,
      'time2' => $time2,
      'Channel' => $Channel,
      'LOGIN_TYPE'  => $LOGIN_TYPE,
      'CHANNELS'    => $CHANNELS,
    ]);
  }
  /**
   *  @brief  : 用户管理-用户登录统计
   *  @return : page
   *  @details: 
   */
  public function trendAction() {
    $LOGIN_TYPE = config('LOGIN_TYPE');
    $CHANNELS = $this->GetAdminChannel();
    //////////////////////////////////////////////////////////////////////////////////////
    $type = $this->request->param('type/d', -1);
    $type = array_key_exists($type, $LOGIN_TYPE) ? $type : -1;
    if ($type != -1) {
      $conds['a.LoginType'] = $type;
    }
    
    $Channel = $this->request->param('Channel/d', -1);
    if ($Channel > 0 && array_key_exists($Channel, $CHANNELS)) {
      $conds['e.Channel'][] = ['IN', array_keys($CHANNELS)];
      $conds['e.Channel'][] = ['EQ', $Channel];
    } else {
      $conds['e.Channel'] = ['IN', array_keys($CHANNELS)];
    }
    ////////////////////////////
    $time1 = $this->request->param('time1/s', '', 'trim,YmdHis2Unix');
    $time2 = $this->request->param('time2/s', '', 'trim,YmdHis2Unix');
    $time2 = ($time2 > 0 && $time2 <= time()) ? $time2 : time();
    $time1 = ($time1 > 0 && $time1 <= $time2) ? $time1 : time() - 15 * 24 * 60 * 60; // 默认半个月的数据
    $time1 = date('Y-m-d 00:00:00', $time1);
    $time2 = date('Y-m-d 23:59:59', $time2);
    $conds['a.LoginTime'] = ['BETWEEN TIME', [$time1, $time2]];
    //////////////////////////////////////////////////////////////////////////////////////
    $sql3 = table('CYRecordDB.dbo.RecordUserLoginLog a', 'rydb.record')->field([
      'CONVERT(VARCHAR(10), [a].[LoginTime], 120)' => 'y',
      'DATEPART(hh, [a].[LoginTime])' => 'h',
      'a.UserID',
    ])
    ->join('CYBasicDB.dbo.AccountsInfo e', 'e.UserID = a.UserID', 'LEFT')
    ->where($conds)->buildSql();
    $sql3 = sql_pure($sql3);
    $list3 = table("{$sql3} k", 'rydb.record')->field(['k.y', 'k.h', 'COUNT([k].[UserID])' => 'c'])->group('k.y, k.h')->order(['y' => 'DESC', 'h'])->select();
    ///////////////////////////////////////////////////////
    $sql4 = table('CYRecordDB.dbo.RecordUserLoginLog a', 'rydb.record')->field([
      'CONVERT(VARCHAR(10), [a].[LoginTime], 120)' => 'y', 'a.UserID',
    ])
    ->join('CYBasicDB.dbo.AccountsInfo e', 'e.UserID = a.UserID', 'LEFT')
    ->where($conds)->buildSql();
    $sql4 = sql_pure($sql4);
    $sql4 = table("{$sql4} k", 'rydb.record')->field(['k.y', 'k.UserID', 'COUNT(k.UserID)' => 'Total'])->group('k.y, k.UserID')->buildSql();
    $sql4 = sql_pure($sql4);
    $list4 = table("{$sql4} w", 'rydb.record')->group('w.y')->order(['y' => 'DESC'])->column(['w.y', 'COUNT(w.y)' => 'Total']);
    ///////////////////////////////////////////////////////
    $path = Request::instance()->baseUrl();
    $total = count($list4);
    $page = new \org\dtcms($list4, ($total <= 0 ? 1 : $total), 1, $total, false, ['path' => $path]);
    //////////////////////////////////////////////////////////////////////////////////////
    $tree = [];
    foreach($list3 as $a) {
      $y = $a['y'];
      if (!isset($tree[$y])) {
        $tree[$y] = ['total' => 0, 'total2' => 0, 'child' => []];
        if (isset($list4[$y])) {
          $tree[$y]['total2'] = $list4[$y]['Total'];
        }
      }
      $tree[$y]['total'] += $a['c'];
      $tree[$y]['child'][$a['h']] = $a;
    }
    return $this->fetch('', [
      'Channel'  => $Channel,
      'type'  => $type,
      'tree'  => $tree,
      'time1' => $time1,
      'time2' => $time2,
      'page'  => $page,
      'LOGIN_TYPE'  => $LOGIN_TYPE,
      'CHANNELS'  => $CHANNELS,
    ]);
  }
}
